package stack;


public class ArryStackDemo {
    public static void main(String[] args) {
        //测试
        ArryStack arryStack = new ArryStack(3);

        arryStack.list();
        arryStack.push(1);
        arryStack.push(2);
        arryStack.push(3);
        arryStack.list();
        System.out.println("------");
        arryStack.pop();
        arryStack.list();


    }
}

//定义一个类表示栈
class ArryStack{
    private int maxSize;  //栈的大小
    private int[] stack;  //数组模拟栈，数据存放在数组中
    private int top = -1; //初始化为-1

    //构造器
    public ArryStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];  //数组要初始化
    }

    //栈满
    public boolean isFull(){
        return top == maxSize-1;
    }

    //栈空
    public boolean isEmpy(){
        return top == -1;
    }

    //入栈
    public void push(int value){
        //判断栈是不是满了
        if(isFull()){
            System.out.println("栈满了");
            return;
        }
        top++;
        stack[top] = value;
    }

    //出栈
    public int pop(){
        //判断栈是不是空
        if(isEmpy()){
            //抛出异常
            throw new RuntimeException("栈空");
        }
        int vaule = stack[top];
        top--;
        return vaule;
    }

    //显示栈(遍历栈)，从上往下显示
    public void list(){
        if(isEmpy()){
            System.out.println("栈空，没有数据");
        }
        for(int i = top; i>=0 ; i--){
            System.out.printf("stack[%d] = %d \n",i,stack[i]);
        }
    }
}
